package Datastruct.stack;

public class ArrayStack {
    //定义栈的主打长度
    private  int maxStack;

    //定义数组
    private  int []stack;

    //定制栈顶指针
    private  int top=-1;

    public ArrayStack(int maxStack) {
        this.maxStack = maxStack;
        stack=new int[maxStack];
    }

    /**
     * 1.压栈
     * 2.出栈
     * 3.判断栈是否为空
     * 4.判断栈是否满
     */

    //判断栈是否为空
    public boolean isEmpty(){
        return  this.top==-1;
    }

    //判断栈是否满
    public  boolean isFull(){
        return this.top==this.maxStack-1;

    }

    //压栈
    public void push(int val){
        //判断栈是否满
        if(isFull()){
            throw  new RuntimeException("栈已满，无法入栈");
        }

        top++;
        stack[top]=val;
    }

    //出栈
    public  int pop(){
        //判断栈是否为空
        if (isEmpty()){
            throw  new RuntimeException("栈为空，无法出栈");
        }
        int val=stack[top];
        top--;

        return val;
    }

    //查看栈中的所有元素
    public  void  listStack(){
        //判断是否为空
        if(isEmpty()){
            throw  new RuntimeException("栈为空");
        }
        for (int i=0;i<stack.length;i++){
            System.out.printf("stack[%d]=%d\n",i,stack[i]);
        }
    }

    //返回栈的长度
    public  int length(){
        return  this.top+1;
    }

    //判断是否是一个运算符  + - * /
    public  boolean isOper(char v){
        return  v=='+'||v=='-'||v=='*'||v=='/';
    }

    //判断运算符的优先级
    public  int priority(char oper){
        if(oper=='*' ||oper=='/'){
            return  1;
        }else  if(oper=='+'||oper=='-'){
            return 0;
        }else {
            return  -1;
        }
    }

    //返回栈顶元素
    public  int peek(){
        return  stack[top];
    }

    //查看栈的容量
    public  int  stackLength(){
        return  this.stack.length;
    }

    //计算两个数的运算结果
    public int calculate(int num1 ,int num2,char oper){
        int result=0;
        switch (oper){
            case '+':
                result=num1+num2;
                break;
            case '-':
                result=num2-num1;
                break;
            case '*':
                result=num1*num2;
                break;
            case '/':
                result=num2/num1;
                break;
        }
        return  result;
    }
}
